package gw.com.android.utils;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.TextView;
import android.os.Handler;

import com.gwtsz.gts2.hx.R;


public class CopyShowerUtil {

    private Context mContext;
    private TextView tv_test;

    private int originalColor = 0xFFFFFFFF;
    private int highLightColor = 0xFFCCCCCC;

    private final static int STYLE_1 = 0X01;//風格1，白色框
    private final static int STYLE_2 = 0X02;//风格2，尖下巴黑色泡泡

    public void setOriginalColor(int originalColor) {
        this.originalColor = originalColor;
    }

    public void setHighLightColor(int highLightColor) {
        this.highLightColor = highLightColor;
    }

    /**
     * 让textView带有长按复制的功能
     *
     * @param context
     * @param tv_test_T
     */
    public CopyShowerUtil(Context context, TextView tv_test_T) {
        mContext = context;
        tv_test = tv_test_T;
        tv_test.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                readyToGotoCopyState(STYLE_2);//暂时选择风格2
                return false;
            }
        });
    }

    private boolean ifCopyState = false;

    /**
     * 获取屏幕寬度
     *
     * @return
     */
    private int getScreenWidth() {
        WindowManager wm = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        return width;
    }

    View layout = null;

    private void readyToGotoCopyState(int style) {
        if (!ifCopyState) {
            tv_test.setBackgroundColor(highLightColor);
            if (style == STYLE_1) {
                layout = LayoutInflater.from(mContext).inflate(R.layout.tv_corner_copy, null);//风格1, 白底文本框
            } else if (style == STYLE_2) {
                layout = LayoutInflater.from(mContext).inflate(R.layout.tv_corner_copy2, null);// 风格2，带尖下巴的气泡状
                int offsetY;//为了保持 让 文案居中，而设置偏移量
                //判断当前屏幕的分辨率是XDPI还是XXDPI
                if (getScreenWidth() <= 720) {//XDPI
                    //XDPI,偏移量應該是15
                    offsetY = 15;
                } else {//xxdpi
                    offsetY = 25;
                }
                layout.setPadding(0, 0, 0, offsetY);
            }

            layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            int height = layout.getMeasuredHeight();
            int width = layout.getMeasuredWidth();
            goToCopyState(height, width);

        }
    }

    /**
     * @param h layout参数的h
     * @param w layout参数的h
     */
    private void goToCopyState(int h, int w) {
        final CopyDialog copyDialog = new CopyDialog(mContext, layout, tv_test, CopyDialog.LOCATION_UPON, h, w);
        layout.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {
                // 控制剪切板，用于复制
                ClipboardManager manager = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                manager.setPrimaryClip(ClipData.newHtmlText(null, tv_test.getText().toString(), null));
                copyDialog.dismiss();
                copyDialog.cancel();
            }
        });
        copyDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                leaveCopyState();
            }
        });
        copyDialog.show();
        ifCopyState = true;
    }

    private void leaveCopyState() {
        if (ifCopyState) {
            tv_test.setBackgroundColor(originalColor);
            ifCopyState = false;
        }
    }
}